var util = require('../../utils/util.js')
var app = getApp();
var md5 = require('../md5.js');
Page({
  data: {
    ellipsis: true,// 文字是否收起，默认收起
    yycgshow: false,
    sfyyshow: false,
    sfxyyyshow: true,
    cureventid: '',
    interval: '',
    countdown:{
      day: '',
      hour: '',
      minute: '',
      second: ''
  },
  },

  // 收起/展开按钮点击事件
  ellipsis() {
    let that = this;
    // let value = !this.data.ellipsis;
    that.setData({
      ellipsis: !that.data.ellipsis
    })
  },
  submit: function () {
    this.setData({
      showModal: true
    })
  },
  onPullDownRefresh: function(){
    wx.stopPullDownRefresh({
      complete: (res) => {},
    });
  },
  //底部导航跳转
  toAiIndex: function () {
    util.toAiIndex()
  },
  toContactsIng: function () {
    util.toContactsIng()
  },
  toProductRevision: function () {
    util.toProductRevision()
  },
  toMy: function () {
    util.toMy()
  },

  toBadge: function () {
    util.toBadge()
  },

  preventTouchMove: function () {

  },


  go: function () {
    this.setData({
      showModal: false
    })
  },
  onLoad: function (options) {
    var that = this;
    var query = wx.createSelectorQuery();
    query.selectAll('.unellipsis-hidden').boundingClientRect(function (rect) {
      rect.forEach((v, i) => {
        console.log("v.height");

        console.log(v.height);
        console.log("i");

        console.log(i);

        if (v.height > 70) {  //判断高度,根据各种高度取折中
          that.setData({
            showDetail: 'true'
          });
        }
        else {
          that.setData({

            showDetail: 'false'
          });
        }
      })
    }).exec();
    var eventid = options.eventid
    this.setData({
      eventid: eventid
    })
    // 页面初始化 options为页面跳转所带来的参数 
    //获取缓存展会id和用户id存在则获取最新联系人列表
    //设置语言0:中文，1：英文
    var setlanguage = wx.getStorageSync('setlanguage')
    if ((wx.getStorageSync('setlanguage') || !"".trim() == wx.getStorageSync('setlanguage'))) {
      this.setData({
        setlanguage: wx.getStorageSync('setlanguage')
      })
      if (setlanguage == 1) {
        wx.setNavigationBarTitle({
          title: 'Activity details'
        })
      } else {
        wx.setNavigationBarTitle({
          title: '活动详情'
        })
      }
    } else {
      wx.setStorageSync('setlanguage', 0);
      this.setData({
        setlanguage: 0
      })
    }
    var exhibitionid = wx.getStorageSync('exhibitionid')
    var u = wx.getStorageSync('u')
    console.log('exhibitionid=' + exhibitionid)
    console.log('u=' + u)
    if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
      && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
      //默认最新（调用pico接口）
      var timestamp = Date.parse(new Date());
      timestamp = timestamp / 1000;
      var url = app.globalData.picoaddress + '//attendee/schedule/detail'
      var k = {}
      k["v"] = '1.0.0'
      k["client"] = 'ios'
      k["timestamp"] = timestamp
      k["lan"] = 'cn'
      k["exhibitionid"] = exhibitionid
      k["eventid"] = eventid
      k["u"] = u

      var sign = app.globalData.picotoken + 'clientios' + 'eventid' + eventid+'exhibitionid' + exhibitionid + 'timestamp' + timestamp + app.globalData.picotoken
      console.log(sign)
      var curb64 = md5.hexMD5(sign)
      console.log(curb64)
      k["sign"] = curb64
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        url: url,
        method: 'post',
        data: k,
        success(res) {
          console.log(res)
          var mesg = res.data
          console.log('mesg=' + mesg)
          var curdata = mesg.data
          that.setData({
            rcDetails: curdata
          });
          that.caculateHeight();
      //    var serverTime = util.formatTime(new Date());
       //   that.startCountdown(serverTime,'1');
        }, fail: res => {
          console.log(res, "error")
        }

      })
    } else {
      wx.redirectTo({
        url: '../signInbyPhone/signIn'
      })
    }

  },
  sc: function (e) {
    var that = this
    var eventid = e.currentTarget.dataset.tagid
    var curscheduleid = that.data.curscheduleid
    var exhibitionid = wx.getStorageSync('exhibitionid')
    var u = wx.getStorageSync('u')
    console.log('exhibitionid=' + exhibitionid)
    console.log('u=' + u)
    if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
      && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
      //默认最新（调用pico接口）
      var timestamp = Date.parse(new Date());
      timestamp = timestamp / 1000;
      var url = app.globalData.picoaddress + '/attendee/schedule/collection'
      var k = {}
      k["v"] = '1.0.0'
      k["client"] = 'ios'
      k["timestamp"] = timestamp
      k["lan"] = 'cn'
      k["exhibitionid"] = exhibitionid
      k["eventid"] = eventid
      k["u"] = u
      var sign = app.globalData.picotoken + 'clientios' + 'eventid' + eventid + 'exhibitionid' + exhibitionid + 'timestamp' + timestamp + app.globalData.picotoken
      console.log(sign)
      var curb64 = md5.hexMD5(sign)
      console.log(curb64)
      k["sign"] = curb64
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        url: url,
        method: 'post',
        data: k,
        success(res) {
          //从新加载数据刷新
          if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
            && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
            //默认最新（调用pico接口）
            var timestamp = Date.parse(new Date());
            timestamp = timestamp / 1000;
            var url = app.globalData.picoaddress + '//attendee/schedule/detail'
            var k = {}
            k["v"] = '1.0.0'
            k["client"] = 'ios'
            k["timestamp"] = timestamp
            k["lan"] = 'cn'
            k["exhibitionid"] = exhibitionid
            k["eventid"] = eventid
            k["u"] = u

            var sign = app.globalData.picotoken + 'clientios' + 'eventid' + eventid + 'exhibitionid' + exhibitionid + 'timestamp' + timestamp + app.globalData.picotoken
            console.log(sign)
            var curb64 = md5.hexMD5(sign)
            console.log(curb64)
            k["sign"] = curb64
            wx.request({
              header: {
                'content-type': 'application/x-www-form-urlencoded',
              },
              url: url,
              method: 'post',
              data: k,
              success(res) {
                console.log(res)
                var mesg = res.data
                console.log('mesg=' + mesg)
                var curdata = mesg.data
                that.setData({
                  rcDetails: curdata
                });
                that.caculateHeight();
              }, fail: res => {
                console.log(res, "error")
              }

            })
          }
        }, fail: res => {
          console.log(res, "error")
        }
      })
    } else {
      wx.redirectTo({
        url: '../signInbyPhone/signIn'
      })
    }
  },
  toSpeaker: function (e) {
    var that = this;
    var speakerid = e.currentTarget.dataset.tagid;
    wx.navigateTo({
      url: '../speechModule/speakerDetails?speakerid=' + speakerid,
    })
  },
  scjs: function (e) {
    var that = this
    var speakerid = e.currentTarget.dataset.tagid
    var exhibitionid = wx.getStorageSync('exhibitionid')
    var eventid = that.data.eventid
    var u = wx.getStorageSync('u')
    console.log('exhibitionid=' + exhibitionid)
    console.log('u=' + u)
    if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
      && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
      //默认最新（调用pico接口）
      var timestamp = Date.parse(new Date());
      timestamp = timestamp / 1000;
      var url = app.globalData.picoaddress + '/attendee/speakers/collect'
      var k = {}
      k["v"] = '1.0.0'
      k["client"] = 'ios'
      k["timestamp"] = timestamp
      k["lan"] = 'cn'
      k["exhibitionid"] = exhibitionid
      k["speakerid"] = speakerid
      k["u"] = u
      var sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'speakerid' + speakerid + 'timestamp' + timestamp + app.globalData.picotoken
      console.log(sign)
      var curb64 = md5.hexMD5(sign)
      console.log(curb64)
      k["sign"] = curb64
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        url: url,
        method: 'post',
        data: k,
        success(res) {
          //从新加载数据刷新
          if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
            && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
            //默认最新（调用pico接口）
            var timestamp = Date.parse(new Date());
            timestamp = timestamp / 1000;
            var url = app.globalData.picoaddress + '//attendee/schedule/detail'
            var k = {}
            k["v"] = '1.0.0'
            k["client"] = 'ios'
            k["timestamp"] = timestamp
            k["lan"] = 'cn'
            k["exhibitionid"] = exhibitionid
            k["eventid"] = eventid
            k["u"] = u

            var sign = app.globalData.picotoken + 'clientios' + 'eventid' + eventid + 'exhibitionid' + exhibitionid + 'timestamp' + timestamp + app.globalData.picotoken
            console.log(sign)
            var curb64 = md5.hexMD5(sign)
            console.log(curb64)
            k["sign"] = curb64
            wx.request({
              header: {
                'content-type': 'application/x-www-form-urlencoded',
              },
              url: url,
              method: 'post',
              data: k,
              success(res) {
                console.log(res)
                var mesg = res.data
                console.log('mesg=' + mesg)
                var curdata = mesg.data
                that.setData({
                  rcDetails: curdata
                });
              }, fail: res => {
                console.log(res, "error")
              }

            })
          }
          that.caculateHeight();
        }, fail: res => {
          console.log(res, "error")
        }
      })
    } else {
      wx.redirectTo({
        url: '../signInbyPhone/signIn'
      })
    }
  },
  caculateHeight: function () {
    var that = this;
    var query = wx.createSelectorQuery();
    query.selectAll('.unellipsis-hidden').boundingClientRect(function (rect) {
      rect.forEach((v, i) => {
        console.log("v.height="+v.height);
        if (v.height > 70) {  //判断高度,根据各种高度取折中
          that.setData({
            showDetail: 'true'
          });
        } else {
          that.setData({
            showDetail: 'false'
          });
        }
      })
    }).exec();
  },
  startCountdown: function (serverTime, endTime) {
    var that = this;
    //var date = "2020-04-21 09:00:00"
    endTime = new Date(endTime.replace(/-/g, '/'));
    
    serverTime = new Date(serverTime);
    var millisecond = endTime.getTime() - serverTime.getTime();
     
    that.data.interval  = setInterval(function () {
      console.log('循环中');
      millisecond -= 1000;
      if (millisecond <= 0){
        clearInterval(that.data.interval);
        that.setData({
          countdown: {
            day: '00',
            hour: '00',
            minute: '00',
            second: '00'
          }
        });
        return;
      }
      that.transformRemainTime(millisecond);
    }, 1000);
  },
  // 剩余时间(毫秒)处理转换时间
  transformRemainTime: function (millisecond) {
    var that = this;
    var countdownObj = that.data.countdown;
    // 秒数
    var seconds = Math.floor(millisecond / 1000);
    // 天数
    countdownObj.day = that.formatTime(Math.floor(seconds / 3600 / 24));
    // 小时
    countdownObj.hour = that.formatTime(Math.floor(seconds / 3600 % 24));
    // 分钟
    countdownObj.minute = that.formatTime(Math.floor(seconds / 60 % 60));
    // 秒
    countdownObj.second = that.formatTime(Math.floor(seconds % 60));
    that.setData({
      countdown: countdownObj
    });
  },
  formatTime: function(time){
    if(time < 10)
      return '0' + time;
    return time;
  },
  confirmyy: function () {
    var that = this
    var cureventid = that.data.cureventid
    console.log('cxszid='+cureventid)
    // var cxszid = cureventid.split('-')[0]+'-'+'1';
    //调用预约接口
    var exhibitionid = wx.getStorageSync('exhibitionid')
    var u = wx.getStorageSync('u')
    console.log('exhibitionid=' + exhibitionid)
    console.log('u=' + u)
    if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
      && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
      //默认最新（调用pico接口）
      var timestamp = Date.parse(new Date());
      timestamp = timestamp / 1000;
      var url = app.globalData.picoaddress + '/attendee/schedule/appointment'
      var k = {}
      k["v"] = '1.0.0'
      k["client"] = 'ios'
      k["timestamp"] = timestamp
      k["lan"] = 'cn'
      k["exhibitionid"] = exhibitionid
      k["eventid"] = cureventid
      k["u"] = u

      var sign = app.globalData.picotoken + 'clientios'+'eventid'+cureventid+'exhibitionid' + exhibitionid + 'timestamp' + timestamp + app.globalData.picotoken
      console.log(sign)
      var curb64 = md5.hexMD5(sign)
      console.log(curb64)
      k["sign"] = curb64
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        url: url,
        method: 'post',
        data: k,
        success(res) {
          that.setData({
            sfyyshow:false,
            yycgshow:true,
          });
        }, fail: res => {
          console.log(res, "error")
        }
      })
    } else {
      wx.redirectTo({
        url: '../signInbyPhone/signIn'
      })
    }
   
  },
  livebro: function (e) {
    var that = this
    clearInterval(that.data.interval)
    var eventMesg = e.currentTarget.dataset.tagid
    var cureventMesgArray = eventMesg.split('_')
    var eventid = cureventMesgArray[0]
    var isappointment = cureventMesgArray[1]
    var livetime =  cureventMesgArray[2]
    //通过livetime截取截止日期
    var endtime = (livetime+'').substring(0,10)
    var curyear = (endtime.split('-'))[0]
    var curmonth = (endtime.split('-'))[1]
    var curday = (endtime.split('-'))[2]
    that.setData({
      curyear:curyear,
      curmonth:curmonth,
      curday:curday
    })
    console.log('endtime='+endtime)
    //var livetime = "2020-04-21 09:00:00"
 //   var cureventid = that.data.cureventid
    var serverTime = util.formatTime(new Date());
    that.startCountdown(serverTime,livetime);
   // var sfyyy= eventArray[1]
    if(isappointment=='true'){
      that.setData({
        sfyyshow:true,
        sfxyyyshow:false,
        cureventid:eventid
      })
    }else{
      that.setData({
        sfyyshow:true,
        sfxyyyshow:true,
        cureventid:eventid
      })
    }
      
  },
  cancle: function () {
    var that = this
    that.setData({
      sfyyshow:false,
    });
  },
  canclesuc: function () {
    var that = this
    that.setData({
      yycgshow:false
    });
    var eventid = that.data.eventid
    this.setData({
      eventid: eventid
    })
    // 页面初始化 options为页面跳转所带来的参数 
    //获取缓存展会id和用户id存在则获取最新联系人列表
    //设置语言0:中文，1：英文
    var setlanguage = wx.getStorageSync('setlanguage')
    if ((wx.getStorageSync('setlanguage') || !"".trim() == wx.getStorageSync('setlanguage'))) {
      this.setData({
        setlanguage: wx.getStorageSync('setlanguage')
      })
      if (setlanguage == 1) {
        wx.setNavigationBarTitle({
          title: 'Activity details'
        })
      } else {
        wx.setNavigationBarTitle({
          title: '活动详情'
        })
      }
    } else {
      wx.setStorageSync('setlanguage', 0);
      this.setData({
        setlanguage: 0
      })
    }
    var exhibitionid = wx.getStorageSync('exhibitionid')
    var u = wx.getStorageSync('u')
    console.log('exhibitionid=' + exhibitionid)
    console.log('u=' + u)
    if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
      && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
      //默认最新（调用pico接口）
      var timestamp = Date.parse(new Date());
      timestamp = timestamp / 1000;
      var url = app.globalData.picoaddress + '//attendee/schedule/detail'
      var k = {}
      k["v"] = '1.0.0'
      k["client"] = 'ios'
      k["timestamp"] = timestamp
      k["lan"] = 'cn'
      k["exhibitionid"] = exhibitionid
      k["eventid"] = eventid
      k["u"] = u

      var sign = app.globalData.picotoken + 'clientios' + 'eventid' + eventid+'exhibitionid' + exhibitionid + 'timestamp' + timestamp + app.globalData.picotoken
      console.log(sign)
      var curb64 = md5.hexMD5(sign)
      console.log(curb64)
      k["sign"] = curb64
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        url: url,
        method: 'post',
        data: k,
        success(res) {
          console.log(res)
          var mesg = res.data
          console.log('mesg=' + mesg)
          var curdata = mesg.data
          that.setData({
            rcDetails: curdata
          });
          that.caculateHeight();
      //    var serverTime = util.formatTime(new Date());
       //   that.startCountdown(serverTime,'1');
        }, fail: res => {
          console.log(res, "error")
        }

      })
    } else {
      wx.redirectTo({
        url: '../signInbyPhone/signIn'
      })
    }
  },
})